<?php
defined('SYSPATH') or die('No direct access allowed.');
class Service_Warninglist {

	//计算预警列表
	/* @time 20140912
     * 可以废弃
	 *
	 * */
    static  Function warninglists($erp_id = NULL,$min=NULL,$max=0)
	{
		$goal = array();

		//获取缓存出错
		try {
			$goal_cache = Cache::instance()->get('result_');
		}
		catch (Cache_Exception $e)
		{
			return NULL;
		}
		if(empty($goal_cache)){
			return NULL;
		}

		//查找结果集
		foreach ($goal_cache as $sku => $goal_cache_sku)
		{
			if(count($goal) >= 100)
			{
				break;
			}

			//有用户id
				if($erp_id)
				{
					if($goal_cache_sku['oper'] == $erp_id)
					{
						if($max and $min)
						{
							if($goal_cache_sku['best'] <= $max and $goal_cache_sku['best'] > $min)
							{
								$goal[$sku] = $goal_cache_sku;
							}
						}
						elseif ($max and $min == NULL)
						{
							if($goal_cache_sku['best'] <= $max)
							{
								$goal[$sku] = $goal_cache_sku;
							}
						}
						else
						{
							if($goal_cache_sku['best'] <= 0)
							{
								$goal[$sku] = $goal_cache_sku;
							}
						}
					}
				}
				else
				{
					if($max and $min)
					{

						if($goal_cache_sku['best'] <= $max and $goal_cache_sku['best'] > $min)
						{
							$goal[$sku] = $goal_cache_sku;
						}

					}
					elseif ($max and $min == NULL)
					{

						if($goal_cache_sku['best'] <= $max)
						{
							$goal[$sku] = $goal_cache_sku;
						}
					}
					else
					{

						if($goal_cache_sku['best'] <= 0)
						{
							$goal[$sku] = $goal_cache_sku;
						}
					}
				}
		}


		//排序
		if(!empty($goal))
		{
			usort($goal, array('Service_Warninglist','cmp'));
		}
		return $goal;

	}


    /*
	//排序，最佳下单日期越早越靠前*/
	static function cmp(array $a,array $b)
	{
		return $a['best']>=$b['best']?+1:-1;
	}

	//查询一个SKU的预警信息
	static function warning_sku($erp_id=NULL,$sku = NULL,$ontime=false)
	{
		$warnings = array();
//      --------------------------------------kdw-----------------------------------
		if($sku)
		{
//            $status_array = array(1,2,3,6,7);//在售状态
//			$skus = DB::select('product_attached.*')
//			->from('product_attached')
//			->join('erp_products_data')
//			->on('product_attached.sku','=','erp_products_data.products_sku')
//			->join('erp_providerproducts')
//			->on('product_attached.sku','=','erp_providerproducts.iproductID')
//			->join('erp_provider')
//			->on('erp_providerproducts.iproviderID','=','erp_provider.id')
//			->where('erp_products_data.products_remark_2','IN',$status_array)
//			->where('erp_provider.state','=',2)
//			->where('product_attached.warning_status','=',0)
//			->where('product_attached.sku','=',$sku);

			if($erp_id)
			{
//				$skus = $skus->where('erp_products_data.oper','=',$erp_id);
                $user=Skusystemapi::getUserSku($sku);
                if($erp_id!=$user[0]['user_id']){
                    return;
                }

			}

            //判断该sku是否为销售状态
            $sku_sale=Skusystemapi::getSaleAndSku($sku);
            if(!count($sku_sale)){
                return;
            }else{
                //判断该sku对应的供应商是否有可用的
                $supplier_sku=Newerpclientapi::querySuppliersku($sku);
                if(count($supplier_sku)){
                    //根据sku查询product_attached表
                    $skus=DB::select('product_attached.*')
                        ->from('product_attached')
                        ->where('warning_status','=',0);
                }
            }

			$skus = $skus
			->execute('readonlyerp')
			->current();
			
			if($skus and !empty($skus))
			{
				
				//是否实时计算
/*				if(flase)
				{
					echo "123";
					$date_end = date('Ymd',strtotime ('now' ) );//echo $date_end;

					$date_start7  = date ( 'Ymd', strtotime ( $date_end. '- 7 day' ) );
					$skus['daysales7'] = Service_daysale::count_daysale($sku,$date_start7,$date_end);

					$date_start14  = date ( 'Ymd', strtotime ( $date_start7. '- 7 day' ) );
					$skus['daysales14'] = Service_daysale::count_daysale($sku,$date_start14,$date_start7)+$skus['daysales7'];

					$date_start21  = date ( 'Ymd', strtotime ( $date_start14. '- 7 day' ) );
					$skus['daysales21'] = Service_daysale::count_daysale($sku,$date_start21,$date_start14)+$skus['daysales14'];

					$date_start28  = date ( 'Ymd', strtotime ( $date_start21. '- 7 day' ) );
					$skus['daysales28'] = Service_daysale::count_daysale($sku,$date_start28,$date_start21)+$skus['daysales21'];
					
					
					$skus['daysales'] = $skus['daysales7']/7*0.5+$skus['daysales14']/14*0.3+$skus['daysales28']/28*0.2;
				}*/
				$warnings[] = Service_warningcount::Warning($skus);
			}
			 
		}
		//var_dump($warnings);exit;
		return $warnings;
	}
	
	static function warning_sku_all($sku = NULL)
	{
		$warnings = array();
		if($sku)
		{
//          --------------------------------------kdw------------------------------------

//			$skus = DB::select('product_attached.*')
//                    ->from('product_attached')
//                    ->join('erp_products_data')
//                    ->on('product_attached.sku','=','erp_products_data.products_sku')
//                    ->join('erp_providerproducts')
//                    ->on('product_attached.sku','=','erp_providerproducts.iproductID')
//                    ->join('erp_provider')
//                    ->on('erp_providerproducts.iproviderID','=','erp_provider.id')
//                    ->where('product_attached.sku','=',$sku);

            //判断sku在售状态
            $skuinfo=Skusystemapi::getSale($sku);
            if(count($skuinfo)){
                //查询sku的销售数据
                $skus=DB::select('product_attached.*')->from('product_attached')->where('sku','=',$sku);
            }else{
                return;
            }
//          --------------------------------------kdw-end------------------------------

			$skus = $skus
			->execute('readonlyerp')
			->current();
				
			if($skus and !empty($skus))
			{
	
				//是否实时计算
// 				if($ontime)
				{
					$date_end = date('Ymd',strtotime ('now' ) );//echo $date_end;
	
					$date_start7  = date ( 'Ymd', strtotime ( $date_end. '- 7 day' ) );
					$skus['daysales7'] = Service_daysale::count_daysale($sku,$date_start7,$date_end);
	
					$date_start14  = date ( 'Ymd', strtotime ( $date_start7. '- 7 day' ) );
					$skus['daysales14'] = Service_daysale::count_daysale($sku,$date_start14,$date_start7)+$skus['daysales7'];
	
					$date_start21  = date ( 'Ymd', strtotime ( $date_start14. '- 7 day' ) );
					$skus['daysales21'] = Service_daysale::count_daysale($sku,$date_start21,$date_start14)+$skus['daysales14'];
	
					$date_start28  = date ( 'Ymd', strtotime ( $date_start21. '- 7 day' ) );
					$skus['daysales28'] = Service_daysale::count_daysale($sku,$date_start28,$date_start21)+$skus['daysales21'];
						
						
					$skus['daysales'] = $skus['daysales7']/7*0.5+$skus['daysales14']/14*0.3+$skus['daysales28']/28*0.2;
				}
				$warnings[] = Service_warningcount::Warning($skus);
			}
	
		}
		//var_dump($warnings);exit;
		return $warnings;
	}
	
/* 	//根据供应商查询
	@time 20140912 可以废弃了*/
    /*
     *
     * 没用用的空方法
     *
     */
 	static function warning_supplier($erp_id=NULL,$supplier_name=NULL)
 	{

 	}
		


}